package com.whfc.emp.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.PageData;
import com.whfc.common.util.PageUtil;
import com.whfc.emp.dao.AppTrainUserAnswerMapper;
import com.whfc.emp.dao.AppTrainingMapper;
import com.whfc.emp.dto.AppTrainingCategoryDTO;
import com.whfc.emp.dto.AppTrainingDTO;
import com.whfc.emp.dto.AppTrainingQuestionsDTO;
import com.whfc.emp.dto.AppTrainingUserAnswerDTO;
import com.whfc.emp.entity.AppTraining;
import com.whfc.emp.entity.AppTrainingUserAnswers;
import com.whfc.emp.param.*;
import com.whfc.emp.service.AppTrainingCategoriesService;
import com.whfc.emp.service.AppTrainingService;
import com.whfc.emp.service.AppTrainingUserAnswerService;
import com.whfc.fuum.service.SysDeptService;
import com.whfc.fuum.service.SysDictService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClasssName AppTrainServiceImpl
 * @Description 人员管理-培训
 * @Author hw
 * @Date 2020/11/26 16:18
 * @Version 1.0
 */
@DubboService(interfaceClass = AppTrainingUserAnswerService.class, version = "1.0.0", timeout = 5000)
public class AppTrainingUserAnswerServiceImpl implements AppTrainingUserAnswerService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AppTrainUserAnswerMapper appTrainingUserAnswersMapper;


    @Override
    public PageData<AppTrainingUserAnswerDTO> list(AppTrainingUserAnswersListParam request) throws BizException {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<AppTrainingUserAnswerDTO> list = appTrainingUserAnswersMapper.selectByParam(request);
        PageHelper.clearPage();
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public void add(AppTrainingAnswerAddParam request) throws BizException {
        AppTrainingUserAnswers dto = new AppTrainingUserAnswers();
        BeanUtils.copyProperties(request, dto);
        AppTrainingUserAnswers old = appTrainingUserAnswersMapper.getAnswerByQuestionIdAndUserId(request.getQuestionId(), request.getUserId(),request.getTrainingId());
        if(old != null){
            old.setSelectedLabels(request.getSelectedLabels());
            old.setSelectedOptionIds(request.getSelectedOptionIds());
            old.setUpdateTime(new Date());
            appTrainingUserAnswersMapper.updateByPrimaryKey(old);
            return;
        }
        appTrainingUserAnswersMapper.insert(dto);
    }

    @Override
    public void edit(AppTrainingEditParam request) throws BizException {

    }

    @Override
    public void del(Integer id) throws BizException {

    }
}
